import http from "@/api";
import { ResPage, User, OrgManager } from "@/api/interface/index";
import qs from "qs";
import { useUserStore } from "@/stores/modules/user";
/**
 * @name 机构管理模块
 */
// 发送邮箱保存
export const getjggl = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }

  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.postitsm<string>("" + `/pages/mailServer!saveOrUpdateEmailServer.action`, qs.stringify(newobj));
};

// 保存公司信息
export const mergeCompany = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }

  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.postitsm<string>("" + `/pages/organization!mergeCompany.action?companyNo=1`, qs.stringify(newobj));
};

// 发送邮箱连接测试
export const emailtest = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/email!emailConnTest.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  // return http.postitsm<string>("" + `/pages/email!emailConnTest.action`, {},  params,header );
};

// 发送邮箱连接测试exchange
export const emailtestexchange = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/email!exchangeEmailConnTest.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  // return http.postitsm<string>("" + `/pages/email!emailConnTest.action`, {},  params,header );
};

// 短信账户
export const testSMSAccount = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/sms!testSMSAccount.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  // return http.postitsm<string>("" + `/pages/email!emailConnTest.action`, {},  params,header );
};

// 公司信息查询
export const getjgglmessage = () => {
  const randomNumber = Math.random();
  let newobj = {};
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.post<ResPage<{}>>(`/pages/organization!findCompany.action?companyNo=1&time=` + randomNumber, qs.stringify(newobj));
};

// 保存公司信息
export const updateOrg = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (`${key}` === "actionType") {
        newobj[`${key}`] = `${params[key]}`;
      } else if (`${key}` === "doTag") {
        newobj[`${key}`] = `${params[key]}`;
      } else if (`${key}` === "orgType") {
        newobj[`${key}`] = `${params[key]}`;
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  }
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.postitsm<string>("" + `/pages/organization!updateOrg.action`, qs.stringify(newobj));
};

// 判断机构是否已经存在
export const isCategoryExisted = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (`${key}` === "actionType") {
        newobj[`${key}`] = `${params[key]}`;
      } else if (`${key}` === "doTag") {
        newobj[`${key}`] = `${params[key]}`;
      } else if (`${key}` === "orgType") {
        newobj[`${key}`] = `${params[key]}`;
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  }
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.postitsm<string>("" + `/pages/organization!isCategoryExisted.action`, qs.stringify(newobj));
};

// 新增机构
export const save = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (`${key}` === "actionType") {
        newobj[`${key}`] = `${params[key]}`;
      } else if (`${key}` === "doTag") {
        newobj[`${key}`] = `${params[key]}`;
      } else if (`${key}` === "orgType") {
        newobj[`${key}`] = `${params[key]}`;
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  }
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.postitsm<string>("" + `/pages/organization!save.action`, qs.stringify(newobj));
};

// 获取用户部门列表
export const getUserDepartmentList = (params: OrgManager.ReqOrgManagerParams) => {
  params.page = params.pageNum;
  params.rows = params.pageSize;
  return http.get(`/pages/organization!find.action?companyNo=1`, {}, { params });
};

// 获取用户部门树形列表
export const getUserDepartment = async () => {
  const userStore = useUserStore();
  if (userStore.departmentTree && Array.isArray(userStore.departmentTree) && userStore.departmentTree.length > 0) {
    return userStore.departmentTree;
  } else {
    const data = await http.get(`/pages/organization!findAllNew.action`, {}, { cancel: false, loading: false });
    if (data && Array.isArray(data)) {
      userStore.setDepartmentTree(data);
    }
    return data;
  }
};

// 更新机构
export const updateOrgWin = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (`${key}` === "actionType") {
        newobj[`${key}`] = `${params[key]}`;
      } else if (`${key}` === "doTag") {
        newobj[`${key}`] = `${params[key]}`;
      } else if (`${key}` === "orgType") {
        newobj[`${key}`] = `${params[key]}`;
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  }
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.postitsm<string>("" + `/pages/organization!updateOrgWin.action`, qs.stringify(newobj));
};

// 删除机构
export const deleteOrgs = (params: {}) => {
  console.log("sssssssssssssssss");
  console.log(params);
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.postitsm<string>("" + `/pages/organization!deleteOrgs.action`, qs.stringify(params));
};

// 查询机构服务时间<ResPage<User.ResUserList>>
export const findServiceTimeByOrgNo = (orgNo: any) => {
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.post<string>(`/pages/serviceTime!findByOrgNo.action?serviceTimeDTO.orgNo=` + orgNo, qs.stringify({}));
};

// 保存或更新服务时间
export const saveOrgUpdate = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/serviceTime!saveOrgUpdate.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  // return http.postitsm<string>("" + `/pages/email!emailConnTest.action`, {},  params,header );
};

/**
 * @name 客户管理模块
 */
// 获取客户列表
export const findITSOPUserPager = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }

        // for (let i = 0; i < unhandle.length; i++) {
        //  if(`${key}`===unhandle[i]){
        //  	newobj[`${key}`] = `${params[key]}`;
        // 	delete newobj[header+"."+`${key}`]
        //  }else{
        //  	newobj[header+"."+`${key}`] = `${params[key]}`;
        // 	delete newobj[`${key}`]
        //  }
        // }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  // return http.post<ResPage<User.ResUserList>>("" + `/pages/itsopUser!findITSOPUserPager.action`, {}, { params });
  return http.postitsm<string>("" + `/pages/itsopUser!findITSOPUserPager.action`, qs.stringify(newobj));
};

// 获取树形用户列表2
export const findUserTreeList = (params: OrgManager.ReqOrgManagerParams) => {
  params.page = params.pageNum;
  params.rows = params.pageSize;
  return http.post<ResPage<User.ResUserList>>(
    `/pages/user!find.action?userQueryDto.companyNo=1&_=1719369189256&userQueryDto.orgNo=` +
      params.departmentId +
      `&userQueryDto.lastName=` +
      (params.fullName == undefined ? "" : params.fullName) +
      `&userQueryDto.loginName=` +
      (params.loginName == undefined ? "" : params.loginName) +
      `&userQueryDto.orgType=` +
      (params.orgType == undefined ? "" : params.orgType),
    {},
    { params }
  );
};

// 更新客户管理信息
export const updateITSOPUser = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/itsopUser!updateITSOPUser.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  // return http.postitsm<string>("" + `/pages/email!emailConnTest.action`, {},  params,header );
};

// 删除客户
export const deleteITSOPUser = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  } else {
    newobj = params;
  }
  return http.post<String>(`/pages/itsopUser!deleteITSOPUser.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  // return http.postitsm<string>("" + `/pages/email!emailConnTest.action`, {},  params,header );
};

// 批量删除客户
export const batchDeleteITSOPUser = (params: string) => {
  return http.post<String>(`/pages/itsopUser!deleteITSOPUser.action` + params, qs.stringify({})); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 更新客户管理信息
export const addITSOPUser = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/itsopUser!addITSOPUser.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  // return http.postitsm<string>("" + `/pages/email!emailConnTest.action`, {},  params,header );
};

// 获取树形用户列表2
export const findOrgById = (params: any) => {
  let param = "";
  if (params.orgNo == "" || params.orgNo == undefined) {
    param = "99999999";
  } else {
    param = params.orgNo;
  }
  console.log(params);
  return http.post<string>(
    `/pages/user!find.action?userQueryDto.companyNo=` +
      param +
      `&_=1719369189256&userQueryDto.orgNo=` +
      `&userQueryDto.lastName=`,
    {},
    {}
  );
};

// 导出模板
export const ITSOPOrganization = (params: OrgManager.ReqOrgManagerParams) => {
  return http.download(`/importFile/ITSOPOrganization.zip`, params);
};

// importITSOPUser
export const importITSOPUser = (params: FormData) => {
  return http.post(`/pages/itsopUser!importITSOPUser.action?fix=0`, params);
};

// 导出数据
export const exportITSOPUser = (params: OrgManager.ReqOrgManagerParams) => {
  let header = "itsopUserQueryDTO";
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.downloaditsm(
    `/pages/itsopUser!exportITSOPUser.action?_search=false&nd=1720518116067&rows=15&page=1&sidx=orgNo&sord=desc`,
    qs.stringify(newobj)
  );
};

// 获取部门节假日
export const getHolidayList = (params: OrgManager.ReqOrgManagerParams) => {
  params.page = params.pageNum;
  params.rows = params.pageSize;
  return http.get(
    `/pages/holiday!findHolidayPage.action?holidayQueryDTO.companyNo=` + (params.companyNo == undefined ? "1" : params.companyNo),
    {},
    { params }
  );
};

// 导出数据
export const findAllSun = (params: string) => {
  return http.post(`/pages/organization!findAll.action?companyNo=1&_=1720751816146&parentOrgNo=` + params, qs.stringify({}));
};

// 获取我支持的客户列表
export const findMySupportITSOPUserPager = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.postitsm<string>(`/pages/itsopUser!findMySupportITSOPUserPager.action`, qs.stringify(newobj));
};

/**
 * @name 查询用户
 */
// 查询用户
export const findUser = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.postitsm<string>(`/pages/user!find.action`, qs.stringify(newobj));
};

// 保存企业微信配置
export const saveWecomConfig = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post(`/pages/wecome!saveWecomConfig.action`, qs.stringify(newobj));
};

// 查询企业微信配置
export const findWecomConfig = () => {
  return http.post(`/pages/wecome!findWecomConfig.action`, qs.stringify({}));
};

// 测试获取accessToken
export const testToken = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post(`/pages/wecome!testToken.action`, qs.stringify(newobj));
};

// 同步资产接口
export const yuehaiImportCi = () => {
  return http.post(`/pages/orgSync!yuehaiImportCi.action`, qs.stringify({}));
};
// 同步资产接口
export const yuehaiImportCi2 = () => {
  return http.post(`/pages/orgSync!yuehaiImportCi2.action`, qs.stringify({}));
};
// 查询邮箱
export const findServereMail = () => {
  return http.post(`/pages/mailServer!findServereMail.action`, qs.stringify({}));
};

// 查询接收邮箱
export const findReceiveServiceMail = () => {
  return http.post(`/pages/mailServer!findReceiveServiceMail.action`, qs.stringify({}));
};
// 查询
export const showPagesSet = () => {
  return http.post(`/pages/pagesSet!showPagesSet.action`, qs.stringify({}));
};

// 更新
export const updatePagesSet = params => {
  return http.post(`/pages/pagesSet!updatePagesSet.action?entity.paName=` + params, qs.stringify({}));
};
